# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import unittest, time
import os

class Test1(unittest.TestCase):
    driver = webdriver.Firefox()

    driver.get("http://www.baidu.com")
    
    driver.save_screenshot('C://b.jpg')         #截图
    oldhandle = driver.current_window_handle
    implement = driver.find_element_by_xpath("//a[@id='lb']").click()
    implement = driver.find_element_by_xpath("//div[contains(concat(' ', @class, ' '), ' bd_dialog_handle ')]")
    print driver
    driver.switch_to_frame("login_iframe")
    #for handle in driver.window_handles:
    #        driver.switch_to_window(handle)
    implement = driver.find_element_by_name("username")
    implement.send_keys("zhengchunxi@foxmail.com")
    implement = driver.find_element_by_name("password")
    implement.send_keys("testMiaozhen")
    implement = driver.find_element_by_id("pass_login_input_submit_0").click() 
    
   
    print driver.title    
    try:
       
        WebDriverWait(driver, 50).until(lambda driver : driver.find_element_by_name("wd"))
        implement = driver.find_element_by_name("wd")
        
        implement.send_keys("sdfsf")
        driver.implicitly_wait(3)
        implement.send_keys('\n')
        driver.implicitly_wait(3)
        implement = driver.find_element_by_name("wd")
        implement.send_keys(u'\ue009' + "a")
        implement.clear()
        implement.send_keys(u'百度空间')
        implement.send_keys('\n')
        

    finally:
        print ("out")  
    implement = driver.find_element_by_xpath("//table[@id='1']//tbody//tr//td//h3//a").click()
    driver.implicitly_wait(30)
    print driver.title
    
    for handle in driver.window_handles:
        driver.switch_to_window(handle)
#    titles = driver.window_handles
#    driver.switch_to_window(titles[1])
    print driver.title       
    driver.implicitly_wait(30)      

    implement = driver.find_element_by_class_name("ent-icon-wraper").click()
    driver.implicitly_wait(30)
    
    implement = driver.find_element_by_class_name("qpub-text")
    
    implement.send_keys(u'这是一个用脚本写的用列')
    implement = driver.find_element_by_xpath("//div[@id='edui31_body']//div[1]").click()
    
    driver.implicitly_wait(30)
    driver.switch_to_frame("edui27_iframe")
    time.sleep(5)
    implement = driver.find_element_by_id("picUpFlash")
    file_path = os.path.abspath("C:\\b.jpg")
    implement.send_keys(file_path)

    

if __name__ == "__main__":
    unittest.main()


